package com.aaa.security;


import com.aaa.entity.Emp;
import com.aaa.service.EmpService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * 获取用户信息
 */
@Repository
public class UserDetailsServiceImpl implements UserDetailsService {

@Resource
private EmpService empService;

//    @Override
//    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
//        System.out.println("s:"+s);
//
//        // 根据用户名获取用户信息
//        if(s.equals("zhangsan")){
//            // 返回正确的用户信息
//            return User.withUsername("zhangsan")
//                    .password(new BCryptPasswordEncoder().encode("123"))
//                    .authorities("ROLE_ADMIN")
//                    .build();
//        }else{
//            throw new UsernameNotFoundException("用户名不存在");
//        }
//    }

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        System.out.println(s);

        List<Emp> list=empService.list(new QueryWrapper<Emp>().eq("eusername",s));
        if (list.size() == 1) {
            Emp emp = list.get(0);
            emp.setAuthorities(AuthorityUtils.commaSeparatedStringToAuthorityList(String.valueOf(emp)));
            return list.get(0);
        } else {
            throw new UsernameNotFoundException("用户名不对");
        }
    }
}
